home *** CD-ROM | disk | FTP | other *** search
- Path: news.clark.net!usenet
- From: yom@clark.net (yom)
- Newsgroups: comp.lang.c
- Subject: Re: changing strings via pointers
- Date: 24 Feb 1996 02:11:53 GMT
- Organization: Your Organization
- Message-ID: <4gls59$rct@clarknet.clark.net>
- References: <1996Feb22.125436.25503@leeds.ac.uk>
- NNTP-Posting-Host: yom.clark.net
- Mime-Version: 1.0
- Content-Type: Text/Plain; charset=US-ASCII
- X-Newsreader: WinVN 0.99.7
-
- You can use strchr string function to get the pointer to a newline
- character in your character array, and set what the pointer points
- to to NULL (0).
-
- char *b;
- if (b = strchr(buffer,'\n'))
- *b = 0;
-
- Song (yom@clark.net)
-
- In article <1996Feb22.125436.25503@leeds.ac.uk>, csyamc@scs.leeds.ac.uk
- says...
- >
- >I reading in strings from a file using fgets, which stores the "\n" in
- >the array of chars which I have declared using a pointer.
- >
- >The thing is, I want to get rid of the "\n" and replace it with "\0",
- but
- >obviously I can only do this using the pointer. I'm using the
- following code:
- >
- >char *Contents < pointer to string
- >
- >int count = 0;
- >
- >
- >while (*(Contents + count)!= NULL) /* the end of string is not
- reached */
- >{if (*(Contents + count) == atoi("\n"))
- >/* change the \n to a \0 and set the next char along to null */
- >{
- >*(Contents + count) = atoi("\0");
- >
- >*(Contents + count + 1) = NULL;
- >
- >
- >}
- >else
- >/* read in the next char */
- >{
- >count++;
- >
- >I think the problem maybe my way of using the pointer - I can print
- out each
- >char as I get to it, but it doesnt seem to change the array at all.
- >Should I be using atoi?, it doesnt seem to work without it.
- >
- >Cheers
- >
- >ANdy
- >
- >
- >
- >
-
-